$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        schoolDg:null,
        dataGrid:null,
        schoolId:null,
        iTTchId:null,
        iUserId:null,
        init:function () {
            this.iTTchId = xhTool.getQuery('iTTchId');
            this.msgId   = xhTool.getQuery('msgId');
            this.schoolDg = new xhDataGrid($('#schoolList'),'tch_log/getSchool',{});
            this.schoolDg.parseToolbar().init();
            this.dataGrid = new xhDataGrid($('#logList'),'tch_log/getTchLog',{iTTchId:this.iTTchId});
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_pass','bt_list_unpass']);
            this.funObj().onLoadSchool();
            this.listener();
            this.funObj().bindDgEvent();

        },
        listener:function () {
            var $this = this;
            //搜索
            $('#bt_TeacherList_Search').click(function () {
                $this.funObj().searchAction();
            });
            xhTool.bindDocKeyDown($('#bt_TeacherList_Search'));
            //审核通过
            $('#bt_list_pass').click(function () {
                $this.funObj().openCheckForm(2);
            });
            //审核不通过
            $('#bt_list_unpass').click(function () {
                $this.funObj().openCheckForm(3);
            })

            $('#bt_SchoolList_Search').click(function () {
                $this.funObj().searchSchool();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                onLoadSchool:function () {
                    $this.schoolDg.onSelect = function(rowIndex,rowData){
                        $this.schoolId = rowData.iSchoolId;
                        $this.dataGrid.reload($this.funObj().getParams())
                            .command('getPanel').panel('setTitle', '日志列表（' + rowData.sSchoolName + '）');
                    };
                    $this.schoolDg.onLoadSuccess = function () {
                        $this.schoolDg.command('selectRow',0);
                    }

                },
                bindDgEvent:function () {
                    $this.dataGrid.onLoadSuccess = function (data) {
                        if (data.rows.length > 0) {
                            $this.dataGrid.mergeCells(['sUserName','tchName']);
                        }
                    }
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        var userId = rowData.iUserId+'_style3Tab2';
                        window.top.xhTop.createNewTab('教师信息', baseUrl+'frontend/Tch/TchTab/'+userId, true);
                    };
                },
                getParams:function () {
                    return {
                        startTime:xhExtra.getValue($('#List_Search_StartTime')),
                        endTime:xhExtra.getValue($('#List_Search_EndTime')),
                        key:xhExtra.getValue($('#TeacherList_Search')),
                        state:xhExtra.getValue($('#select_state')),
                        schoolId:$this.schoolDg.getSelectValue('iSchoolId'),
                        iTTchId:$this.iTTchId
                    }
                },
                searchAction:function () {
                    $this.dataGrid.reload($this.funObj().getParams());
                },
                openCheckForm:function (state) {
                    var ids = $this.dataGrid.getSelectValues('iTTchId');
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: state == 2 ?'通过' : '不通过' ,
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '审批意见',
                        input: 'textarea',
                        preConfirm: function(explain) {
                            return new Promise(function (resolve, reject) {
                                if(!explain){
                                    reject('说明不能为空！');
                                }
                                else {
                                    resolve(explain);
                                }
                            })
                        }
                    }).then(function(explain) {
                        $this.funObj().saveExplain(ids,explain,state);
                    }, function(dismiss) {});
                },
                saveExplain:function (ids,explain,state) {
                    xhExtra.ajaxRequest('tch_log/checkLog',{ids:ids,desc:explain,state:state,msgId:$this.msgId},{},function (res) {
                        $this.dataGrid.reload();
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                searchSchool:function () {
                    $this.schoolDg.reload({
                        key:xhExtra.getValue($('#SchoolList_Search'))
                    });
                }
            }
        }
    }
};
